package cn.wolfcode.web.controller;

import cn.wolfcode.domain.MessageBoard;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IMessageBoardService;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/index")
public class MessageBoardController {
    @Autowired
    private IMessageBoardService messageBoardService;
    @RequestMapping("/listBoard")
    @RequiredPermission(name="留言板查询",expression = "messageBoard:list")
    public String list(Model model,@ModelAttribute("qo") QueryObject qo){
        PageInfo<MessageBoard> pageInfo = messageBoardService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        return "board";


//                List<MessageBoard> messageBoards = messageBoardService.listAll();
//        model.addAttribute("messageBoards",messageBoards);
//        return "messageBoard/list";  //找视图 / WEB-INF/views/messageBoard/list.jsp
    }
    @RequestMapping("/delete")
    @RequiredPermission(name="留言板删除",expression = "messageBoard:delete")
    public String delete(Long id){
        if (id!=null) {
            messageBoardService.delete(id);
        }
        return "redirect:/messageBoard/list";  //让浏览器重新发一次请求
    }
    @RequestMapping("/input")
    @RequiredPermission(name="留言板新增或修改",expression = "messageBoard:input")
    public String input(Long id,Model model){
        if (id!=null) {
            MessageBoard messageBoard = messageBoardService.get(id);
            model.addAttribute("messageBoard",messageBoard);
        }
        return "messageBoard/input";
    }

    @RequestMapping("/saveOrUpdate")
    @RequiredPermission(name="留言板新增或修改",expression = "messageBoard:saveOrUpdate")
    public String saveOrUpdate(MessageBoard messageBoard){
        if (messageBoard.getId()!=null) {
            messageBoardService.update(messageBoard);
        }else {
          messageBoardService.save(messageBoard);
        }
        return "redirect:/messageBoard/list";
    }
}
